﻿using lwzlz.Application.System;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using lzwlz.Web;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.Mall.Controllers
{
    public class AddressController : MallController
    {
        #region 界面
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(string id = "")
        {
            var model = new Address();

            ViewData["ProvinceList"] = new PlaceService().GetDeepList(1);
            ViewData["CityList"] = "";
            if (!string.IsNullOrWhiteSpace(id) && int.TryParse(id, out int idInt))
            {
                model = new AddressService().Get(idInt);
                ViewData["CityList"] = new PlaceService().GetSelectList(model.ProvinceCode);
            }

            return View(model);
        }
        #endregion

        #region 接口
        /// <summary>
        /// 地址列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddressList(int pageNumber, int pageSize, int? categoryId = null, string keyword = null)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var addressService = new AddressService();
                var dic = new Dictionary<string, string>();
                //会员ID
                {
                    dic.Add("MemberId", MallMemberLogin.GetCurrentUser().Id.ToString());
                }
                //关键词
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    dic.Add("RealName", keyword);
                }

                var list = addressService.GetListPage(pageNumber, pageSize, dic, null, null, out int count);

                response.Data = new { List = list, Count = count, };
                response.Success = true;
                response.Message = "获取成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 删除地址
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteAddress(string id)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var addressService = new AddressService();
                addressService.DeleteList(id.ToString());
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 获取城市列表
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public JsonResult GetCityList(string code)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var cityList = new PlaceService().GetSelectList(code);
                response.Success = true;
                response.Message = "获取成功";
                response.Data = cityList;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="RealName"></param>
        /// <param name="Mobile"></param>
        /// <param name="SendAddress"></param>
        /// <param name="ProvinceCode"></param>
        /// <param name="ProvinceName"></param>
        /// <param name="CityCode"></param>
        /// <param name="CityName"></param>
        /// <param name="PostCode"></param>
        /// <returns></returns>
        public JsonResult Save(string Id, string MemberId, string RealName, string Mobile, string SendAddress, string ProvinceCode, string ProvinceName, string CityCode, string CityName, string PostCode)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var addressService = new AddressService();
                if (!string.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out int id) && id > 0)
                {
                    var entity = addressService.Get(id);
                    entity.RealName = RealName;
                    entity.Mobile = Mobile;
                    entity.SendAddress = SendAddress;
                    entity.ProvinceCode = ProvinceCode;
                    entity.ProvinceName = ProvinceName;
                    entity.CityCode = CityCode;
                    entity.CityName = CityName;
                    entity.PostCode = PostCode;
                    entity.LastModificationTime = DateTime.Now;
                    var result = addressService.Update(entity, null);
                    if (result > 0)
                    {
                        response.Success = true;
                        response.Message = "保存成功";
                        response.Data = Url.Action("List", "Address");
                    }
                    else
                    {
                        throw new Exception("保存失败");
                    }
                }
                else
                {
                    var entity = new Address()
                    {
                        MemberId = int.Parse(MemberId),
                        RealName = RealName,
                        Mobile = Mobile,
                        SendAddress = SendAddress,
                        ProvinceCode = ProvinceCode,
                        ProvinceName = ProvinceName,
                        CityCode = CityCode,
                        CityName = CityName,
                        PostCode = PostCode,
                        CreationTime = DateTime.Now
                    };
                    var result = addressService.Insert(entity, null);
                    if (result.HasValue && result.Value > 0)
                    {
                        response.Success = true;
                        response.Message = "保存成功";
                        response.Data = Url.Action("List", "Address");
                    }
                    else
                    {
                        throw new Exception("保存失败");
                    }
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion
    }
}